package com.cat.simulation;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/valid-sudoku/?envType=daily-question&envId=2025-08-30
 * @create 2025/8/30 17:14
 * @since JDK17
 */

public class Solution07 {
    public boolean isValidSudoku(char[][] board) {
        int n = board.length;
        boolean[] h1 = new boolean[n], h2 = new boolean[n], h3 = new boolean[n];
        char a, b, c;
        for (int i = 0; i < n; i++) {
            Arrays.fill(h1, false);
            Arrays.fill(h2, false);
            Arrays.fill(h3, false);
            for (int j = 0; j < n; j++) {
                a = board[i][j];
                b = board[j][i];
                c = board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3];
                if (a != '.') {
                    if (h1[a - '0']) {
                        return false;
                    } else {
                        h1[a - '0'] = true;
                    }
                }
                if (b != '.') {
                    if (h2[b - '0']) {
                        return false;
                    } else {
                        h2[b - '0'] = true;
                    }
                }
                if (c != '.') {
                    if (h3[c - '0']) {
                        return false;
                    } else {
                        h3[c - '0'] = true;
                    }
                }
            }
        }
        return true;
    }
}
